#!/usr/bin/python
# -*- coding:utf-8 -*-
# @author  : micah
# @time    :  
# @function: 
# @version :


import asyncio


# 链接多个数据库为例
class AsyncContexManager:
    def __init__(self, conn=None):
        self.conn = conn

    # 创建数据量异步读写任务
    async def do_something(self):
        return '模拟多个数据库完成增删改查并返回结果的操作...'

    # 使用with自动链接数据库
    async def __aenter__(self):
        print('__aneter__方法执行了...')
        self.conn = await asyncio.sleep(1,result=200)
        return self

    # 如果代码执行完毕自动断开数据库链接
    async def __aexit__(self,exc_type,exc_val, exc_tb):
        print('__aexit__方法执行了..')
        await asyncio.sleep(1)


async def main():
    async with AsyncContexManager() as fp:
        result = await fp.do_something()
        print(result)


asyncio.run(main())
